package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.requestbean.BaseResponseBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.ClearCustomerRequestFilter;
import com.mmlc.network.filter.CustomerListRequestFilter;
import com.mmlc.network.responsebean.CustomerListResponseBean;
import com.mmlc.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/28.
 */
public class CustomerManageActivity extends InterActivity implements View.OnClickListener {

    @InjectView(R.id.iv_back)
    ImageView back;
    @InjectView(R.id.iv_delete)
    ImageView iv_delete;
    @InjectView(R.id.list_customer)
    ListView listcustomer;
    @InjectView(R.id.tv_add)
    TextView tv_add;


    List<CustomerListResponseBean.DataBean> datas;
    private CustomerAdapter adapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_customer_manage;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        initUI();
    }

    private void initUI() {
        datas = new ArrayList<>();
        adapter = new CustomerAdapter(context,datas);
        listcustomer.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        CustomerListRequestFilter filter = new CustomerListRequestFilter((BaseParentActivity)activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CustomerListResponseBean>() {
            @Override
            public void onSuccess(CustomerListResponseBean result) {
                displayCustomerList(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("数据有误，请重新进入");
            }
        });
    }


    private void displayCustomerList(CustomerListResponseBean result) {
        datas.clear();
        datas.addAll(result.getData());
        if (datas.size() > 0) iv_delete.setVisibility(View.VISIBLE);

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initEvent() {
        if (datas != null && datas.size() > 0) iv_delete.setVisibility(View.VISIBLE);
        else iv_delete.setVisibility(View.GONE);


        adapter.setCustomerClickListener(new CustomerAdapter.OnCustomerClickListener() {
            @Override
            public void onCheckChanged(CompoundButton buttonView, boolean isChecked) {

            }

            @Override
            public void onClickContent(View v, int position, boolean isCheck) {
                if (!isCheck) {
                    Intent intent = new Intent(activity, EditCustomerActivity.class);
                    /** type 0:添加；1：修改 */
                    intent.putExtra("id", datas.get(position).getId());
                    intent.putExtra("type", 1);
                    intent.putExtra("name", datas.get(position).getCustomer_name());
                    intent.putExtra("phone", datas.get(position).getCustomer_phone());
                    intent.putExtra("position",position);
                    startActivityForResult(intent, 201);
                }
            }
        });
    }

    boolean isDeleting = false;

    @OnClick({R.id.iv_back,R.id.iv_delete,R.id.tv_add})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                back();
                break;
            case R.id.iv_delete:
                if (!isDeleting) {
                    isDeleting = !isDeleting;
                    adapter.setCheck(true);
                    tv_add.setVisibility(View.GONE);
                } else {
                    for (int i = listcustomer.getChildCount() - 1; i >= 0; i--) {
                        if (((CheckBox) listcustomer.getChildAt(i).findViewById(R.id.checkbox)).isChecked()) {
                            deleteCustomer(datas.get(i));
                            datas.remove(i);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    if (datas.size() == 0) {
                        iv_delete.setVisibility(View.GONE);
                        isDeleting = !isDeleting;
                        adapter.setCheck(false);
                    }
                }
                break;
            case R.id.tv_add:
                Intent intent = new Intent(activity, ActivityAddCustomer.class);
                /** type 0:添加；1：修改 */
                intent.putExtra("type", 0);
//                startActivity(intent);
                startActivityForResult(intent, 201);
                break;
        }
    }

    private void back() {
        if (!isDeleting) {
            finish();
        } else {
            isDeleting = !isDeleting;
            adapter.setCheck(false);
            tv_add.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        back();
    }

    private void deleteCustomer(CustomerListResponseBean.DataBean dataBean) {
        ClearCustomerRequestFilter filter = new ClearCustomerRequestFilter(activity);
        filter.requestBean.id = dataBean.getId();
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BaseResponseBean>() {
            @Override
            public void onSuccess(BaseResponseBean result) {
                ToastUtils.showToast("删除成功");
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            onResume();
        }
    }

}
